/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dialog;

import main.Demo;
import dto.ActiveEmail;
import java.awt.Color;
import java.awt.SystemColor;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
import util.EmailValidator;
import util.RandomNumber;
import util.ReadWriteFile;
import util.SendMail;
import xml.XmlFile4;

/**
 *
 * @author phat10130059
 */
public class CreatAccount extends javax.swing.JPanel {

    /**
     * Creates new form CreatAccount
     */
    private EmailValidator emailValidator;
    private JDialog jdialog;
    private Demo demo;
    private String code;
    private ActiveEmail activeEmail;

    public CreatAccount(JDialog jdialog, Demo demo) {

        this.demo = demo;

        initComponents();

        this.jdialog = jdialog;
        
        jTabbedPane1.setTitleAt(0, demo.TitleTextTab1CreateAcount);
        jTabbedPane1.setTitleAt(1, demo.TitleTextTab2CreateAcount);

        emailValidator = new EmailValidator();


        initEmail();


        File fXmlFile = new File(demo.filePath + File.separator + "fileEmail.xml");
        if (fXmlFile.exists() && !fXmlFile.isDirectory()) {

            activeEmail = XmlFile4.readFileXml(demo.filePath + File.separator + "fileEmail.xml");
            System.out.println(activeEmail);
            if (activeEmail.getActive().equals("0")) {

                txtEmail.setText(activeEmail.getEmail());
                txtReEmail.setText(activeEmail.getEmail());

                lbCode.setVisible(true);
                txtCode.setVisible(true);

                btSendEmail.setVisible(true);
                btSendEmail.setEnabled(true);
                //send mail again
                btSendEmail.setText(demo.BtSendEmail3);

            } else if (activeEmail.getActive().equals("1")) {
                lbCode.setVisible(false);
                txtCode.setVisible(false);

                lbReEmail.setVisible(false);
                txtReEmail.setVisible(false);

                if (demo.langPo) {
                    lbEmail.setText("Email cadastrado");
                } else {
                    lbEmail.setText("Email registered");
                }
                txtEmail.setText(activeEmail.getEmail());
                txtEmail.setEnabled(false);
                txtEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));


                //new mail
                btSendEmail.setText(demo.BtSendEmail2);
                btSendEmail.setVisible(true);
                btSendEmail.setEnabled(true);

                initTabPassword();
            }


        }



    }

    private void initTabPassword() {
        String env = System.getenv("USERPROFILE").toString();
        String pass = ReadWriteFile.read(env + File.separator + "p.phat").trim();
        if (!pass.equals("123")) {
            txtPassActive.setText("");
            txtPassActive.setEditable(true);

            final JTextComponent tcA = (JTextComponent) txtPassActive;
            tcA.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void insertUpdate(DocumentEvent e) {
                    initActionBtOk();
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    initActionBtOk();
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                }
            });

        }

        txtPassNew.setEditable(true);
        final JTextComponent tcPassNew = (JTextComponent) txtPassNew;
        tcPassNew.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                initActionBtOk();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                initActionBtOk();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });


        txtRePassNew.setEditable(true);
        final JTextComponent tcRePassNew = (JTextComponent) txtRePassNew;
        tcRePassNew.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                initActionBtOk();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                initActionBtOk();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });


    }

    private void initActionBtOk() {
        if (!txtPassActive.getText().trim().equals("")
                && !txtPassNew.getText().trim().equals("")
                && !txtRePassNew.getText().trim().equals("")) {

            btOk.setEnabled(true);

        } else {
            btOk.setEnabled(false);
        }
    }

    private void initEmail() {

        final JTextComponent tcA = (JTextComponent) txtEmail;
        tcA.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {

                boolean validateEmail = emailValidator.validate(txtEmail.getText().trim());
                if (validateEmail) {

                    if (activeEmail != null) {
                        if (!activeEmail.getEmail().equals(txtEmail.getText().trim())
                                || activeEmail.getActive().equals("0")) {

                            txtEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));

                        } else {
                            txtEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                        }
                    } else {
                        txtEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                    }

                } else {
                    txtEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                }

            }

            @Override
            public void removeUpdate(DocumentEvent e) {

                String t = txtEmail.getText().trim();


                if (t.trim().equals("")) {
                    txtEmail.setBorder(BorderFactory.createLineBorder(SystemColor.control));
                } else {
                    boolean validateEmail = emailValidator.validate(txtEmail.getText().trim());
                    if (validateEmail) {
                        if (activeEmail != null) {
                            if (!activeEmail.getEmail().equals(txtEmail.getText().trim())
                                    || activeEmail.getActive().equals("0")) {

                                txtEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));

                            } else {
                                txtEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                            }
                        } else {
                            txtEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                        }
                    } else {
                        txtEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                    }
                }



            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });



        //init txtReEmail
        final JTextComponent tcReEmail = (JTextComponent) txtReEmail;
        tcReEmail.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {

                boolean validateEmail = emailValidator.validate(txtReEmail.getText().trim());
                if (validateEmail
                        && (txtReEmail.getText().trim().equals(txtEmail.getText().trim()))) {

                    if (activeEmail != null) {
                        if (!activeEmail.getEmail().equals(txtReEmail.getText().trim())
                                || activeEmail.getActive().equals("0")) {
                            txtReEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                            btSendEmail.setEnabled(true);
                        } else {
                            btSendEmail.setEnabled(false);
                            txtReEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                        }
                    } else {
                        txtReEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                        btSendEmail.setEnabled(true);
                    }

                } else {
                    btSendEmail.setEnabled(false);
                    txtReEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {

                String t = txtReEmail.getText().trim();
                if (t.trim().equals("")) {
                    txtReEmail.setBorder(BorderFactory.createLineBorder(SystemColor.control));
                    btSendEmail.setEnabled(false);
                } else {
                    boolean validateEmail = emailValidator.validate(txtReEmail.getText().trim());
                    if (validateEmail
                            && (txtReEmail.getText().trim().equals(txtEmail.getText().trim()))) {



                        if (activeEmail != null) {
                            if (!activeEmail.getEmail().equals(txtReEmail.getText().trim())
                                    || activeEmail.getActive().equals("0")) {
                                txtReEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                                btSendEmail.setEnabled(true);
                            } else {
                                btSendEmail.setEnabled(false);
                                txtReEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                            }
                        } else {
                            txtReEmail.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                            btSendEmail.setEnabled(true);
                        }

                    } else {
                        btSendEmail.setEnabled(false);
                        txtReEmail.setBorder(BorderFactory.createLineBorder(Color.RED));
                    }
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });

        final JTextComponent tcCode = (JTextComponent) txtCode;
        tcCode.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                activeCode();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                activeCode();

            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });

    }

    public void activeCode() {
        if (txtCode.getText().trim().equals(activeEmail.getCode())) {

            lbCode.setVisible(false);
            txtCode.setVisible(false);
            lbReEmail.setVisible(false);
            txtReEmail.setVisible(false);

            if (demo.langPo) {
                lbEmail.setText("Email cadastrado");
            } else {
                lbEmail.setText("Email registered");
            }


            txtEmail.setText(activeEmail.getEmail());
            txtEmail.setEnabled(false);

            if (demo.langPo) {
                btSendEmail.setText("Trocar email");
            } else {
                btSendEmail.setText("Email Exchange");
            }



            btSendEmail.setVisible(true);
            btSendEmail.setEnabled(true);

            activeEmail.setActive("1");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    XmlFile4.createXmlNew(demo.filePath + File.separator + "fileEmail.xml", activeEmail);
                }
            }).start();

            if (demo.langPo) {
                JOptionPane.showMessageDialog(this, "Email cadastrado com sucesso");
            } else {
                JOptionPane.showMessageDialog(this, "Email successfully registered");
            }


            this.demo.btCreatePass.setText(activeEmail.getEmail());

            initTabPassword();

            jTabbedPane1.setSelectedIndex(1);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lbInfo = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        lbEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lbReEmail = new javax.swing.JLabel();
        txtReEmail = new javax.swing.JTextField();
        lbCode = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        btSendEmail = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtPassActive = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtPassNew = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtRePassNew = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        btOk = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 40));
        jPanel1.setLayout(new java.awt.BorderLayout());

        lbInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbInfo.setText(demo.LbInfoTab1);
        lbInfo.setPreferredSize(new java.awt.Dimension(344, 30));
        jPanel1.add(lbInfo, java.awt.BorderLayout.CENTER);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTabbedPane1MouseClicked(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(demo.BorderTab1CreateAcount));
        jPanel2.setLayout(new java.awt.GridLayout(7, 1));

        lbEmail.setText("Email:");
        jPanel2.add(lbEmail);

        txtEmail.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.SystemColor.control));
        jPanel2.add(txtEmail);

        lbReEmail.setText(demo.LbReEmail);
        jPanel2.add(lbReEmail);

        txtReEmail.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.SystemColor.control));
        jPanel2.add(txtReEmail);

        lbCode.setForeground(new java.awt.Color(255, 51, 0));
        lbCode.setText(demo.LbCode);
        lbCode.setVisible(false);
        jPanel2.add(lbCode);

        txtCode.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.SystemColor.control));
        txtCode.setVisible(false);
        jPanel2.add(txtCode);

        btSendEmail.setText(demo.BtSendEmail1);
        btSendEmail.setEnabled(false);
        btSendEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSendEmailActionPerformed(evt);
            }
        });
        jPanel2.add(btSendEmail);

        jTabbedPane1.addTab("Configurar conta", jPanel2);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(demo.BorderTab2CreateAcount));
        jPanel3.setLayout(new java.awt.GridLayout(7, 1));

        jLabel5.setText(demo.JLabel5Account);
        jPanel3.add(jLabel5);

        txtPassActive.setEditable(false);
        txtPassActive.setText("123");
        jPanel3.add(txtPassActive);

        jLabel6.setText(demo.JLabel6Account);
        jPanel3.add(jLabel6);

        txtPassNew.setEditable(false);
        jPanel3.add(txtPassNew);

        jLabel7.setText(demo.JLabel7Account);
        jPanel3.add(jLabel7);

        txtRePassNew.setEditable(false);
        jPanel3.add(txtRePassNew);

        jTabbedPane1.addTab("Trocar senha", jPanel3);

        jTabbedPane1.setSize(100, 100);

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        jPanel4.setPreferredSize(new java.awt.Dimension(417, 40));

        btOk.setEnabled(false);
        btOk.setText("Ok");
        btOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOkActionPerformed(evt);
            }
        });

        jButton2.setText(demo.BtCancel);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(197, Short.MAX_VALUE)
                .addComponent(btOk, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btOk)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel4, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jTabbedPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPane1MouseClicked

        if (jTabbedPane1.getSelectedIndex() == 0) {
            lbInfo.setText(demo.LbInfoTab1);
        } else {
            lbInfo.setText(demo.LbInfoTab2);
        }

    }//GEN-LAST:event_jTabbedPane1MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jdialog.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private boolean sendEmail() {

//        String email = "codigo@emailexclusivo.com.br";
//        String pass = "Mn@3101!@#$";

        String email = "codigoguardiaoexclusivo@gmail.com";
        String pass = "ExclusivO@1957@@";

        code = "" + RandomNumber.number();

        try {
            SendMail.gmailSend(txtEmail.getText().trim(),
                    email, pass, "GUARDIÃO EXCLUSIVO - Confirmação de Email",
                    "Olá!<br><br>Recebemos uma solicitação para criar uma conta do GUARDIÃO EXCLUSIVO associada ao email " + txtEmail.getText().trim() + ".<br><br>"
                    + "Para validar o email, digite <font color=\"#CC0000\"><b>" + code + "</b></font> "
                    + "no campo \"Código de confirmação\" da janela do GUARDIÃO EXCLUSIVO, conforme imagem abaixo:");
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

    }

    private boolean sendEmailAgain() {

//        String email = "codigo@emailexclusivo.com.br";
//        String pass = "Mn@3101!@#$";

        String email = "codigoguardiaoexclusivo@gmail.com";
        String pass = "ExclusivO@1957@@";


        try {
            SendMail.gmailSend(activeEmail.getEmail(),
                    email, pass, "GUARDIÃO EXCLUSIVO - Confirmação de Email",
                    "Olá!<br><br>Recebemos uma solicitação para criar uma conta do GUARDIÃO EXCLUSIVO associada ao email " + activeEmail.getEmail() + ".<br><br>"
                    + "Para validar o email, digite <font color=\"#CC0000\"><b>" + activeEmail.getCode() + "</b></font> "
                    + "no campo \"Código de confirmação\" da janela do GUARDIÃO EXCLUSIVO, conforme imagem abaixo:");
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }


    }

    private void btSendEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSendEmailActionPerformed


        if (demo.langPo) {
            actionPo();
        } else {
            actionEn();
        }

    }//GEN-LAST:event_btSendEmailActionPerformed

    public void actionPo() {
        if (btSendEmail.getText().equals("Trocar email")) {
            lbEmail.setText("Novo email:");

            txtEmail.setText("");
            txtEmail.setEnabled(true);

            lbReEmail.setVisible(true);

            txtReEmail.setText("");
            txtReEmail.setVisible(true);


            lbCode.setVisible(false);
            txtCode.setVisible(false);

            btSendEmail.setVisible(true);
            btSendEmail.setEnabled(false);
            btSendEmail.setText("Enviar código de confirmação no email acima");



        } else if (btSendEmail.getText().equals("Enviar email novamente")) {

            boolean sendEmailAgain = sendEmailAgain();
            if (sendEmailAgain) {
                String t = "<html>Um código de confirmação foi enviado para o email <br>"
                        + activeEmail.getEmail() + ". <br><br>"
                        + "Verifique seu email, veja o código e digite-o na janela abaixo.<br><br>"
                        + "Se o email com o código não estiver na sua Caixa de Entrada,"
                        + "<br> procure nas pastas Lixo, Junk ou Spam.";
                JOptionPane.showMessageDialog(this, t);
            } else {
                JOptionPane.showMessageDialog(this, "Enviar email de erro", "Error", JOptionPane.ERROR_MESSAGE);
            }

        } else {
            boolean validateEmail = emailValidator.validate(txtEmail.getText().trim());
            boolean validateReEmail = emailValidator.validate(txtReEmail.getText().trim());
            if (validateEmail && validateReEmail) {
                if (txtEmail.getText().trim().equals(txtReEmail.getText().trim())) {
                    boolean sendEmail = sendEmail();
                    if (sendEmail) {

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                ActiveEmail activeE = new ActiveEmail();
                                activeE.setEmail(txtEmail.getText().trim());
                                activeE.setCode(code);
                                activeE.setActive("0");

                                XmlFile4.createXmlNew(demo.filePath + File.separator + "fileEmail.xml", activeE);

                                activeEmail = XmlFile4.readFileXml(demo.filePath + File.separator + "fileEmail.xml");
                            }
                        }).start();



                        String t = "<html>Um código de confirmação foi enviado para o email <br>"
                                + txtEmail.getText().trim() + ". <br><br>"
                                + "Verifique seu email, veja o código e digite-o na janela abaixo.<br><br>"
                                + "Se o email com o código não estiver na sua Caixa de Entrada,"
                                + "<br> procure nas pastas Lixo, Junk ou Spam.";
                        JOptionPane.showMessageDialog(this, t);

                        btSendEmail.setVisible(true);
                        btSendEmail.setEnabled(true);
                        btSendEmail.setText("Enviar email novamente");

                        lbCode.setVisible(true);
                        txtCode.setVisible(true);

                        this.demo.btCreatePass.setText("Criar uma conta");



                    } else {
                        JOptionPane.showMessageDialog(this, "Enviar email de erro", "Error", JOptionPane.ERROR_MESSAGE);
                    }

                } else {
                    JOptionPane.showMessageDialog(this, "Digite novamente não é válido", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(txtCode, "Email não é válido", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void actionEn() {
        if (btSendEmail.getText().equals("Email Exchange")) {
            lbEmail.setText("New email:");

            txtEmail.setText("");
            txtEmail.setEnabled(true);

            lbReEmail.setVisible(true);

            txtReEmail.setText("");
            txtReEmail.setVisible(true);


            lbCode.setVisible(false);
            txtCode.setVisible(false);

            btSendEmail.setVisible(true);
            btSendEmail.setEnabled(false);
            btSendEmail.setText("Send email the confirmation code above");



        } else if (btSendEmail.getText().equals("Send email email again")) {

            boolean sendEmailAgain = sendEmailAgain();
            if (sendEmailAgain) {
                String t = "<html>A confirmation code has been sent to your email <br>"
                        + activeEmail.getEmail() + ". <br><br>"
                        + "Check your email, look at the code and enter it in the window below.<br><br>"
                        + "If the email with the code is not in your Inbox,"
                        + "<br> look in the Trash, Junk or Spam folders.";
                JOptionPane.showMessageDialog(this, t);
            } else {
                JOptionPane.showMessageDialog(this, "Email error", "Error", JOptionPane.ERROR_MESSAGE);
            }

        } else {
            boolean validateEmail = emailValidator.validate(txtEmail.getText().trim());
            boolean validateReEmail = emailValidator.validate(txtReEmail.getText().trim());
            if (validateEmail && validateReEmail) {
                if (txtEmail.getText().trim().equals(txtReEmail.getText().trim())) {
                    boolean sendEmail = sendEmail();
                    if (sendEmail) {

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                ActiveEmail activeE = new ActiveEmail();
                                activeE.setEmail(txtEmail.getText().trim());
                                activeE.setCode(code);
                                activeE.setActive("0");

                                XmlFile4.createXmlNew(demo.filePath + File.separator + "fileEmail.xml", activeE);

                                activeEmail = XmlFile4.readFileXml(demo.filePath + File.separator + "fileEmail.xml");
                            }
                        }).start();



                        String t = "<html>A confirmation code has been sent to your email <br>"
                                + txtEmail.getText().trim() + ". <br><br>"
                                + "Check your email, look at the code and enter it in the window below.<br><br>"
                                + "If the email with the code is not in your Inbox,"
                                + "<br> look in the Trash, Junk or Spam folders.";
                        JOptionPane.showMessageDialog(this, t);

                        btSendEmail.setVisible(true);
                        btSendEmail.setEnabled(true);
                        btSendEmail.setText("Send email email again");

                        lbCode.setVisible(true);
                        txtCode.setVisible(true);

                        this.demo.btCreatePass.setText("Create an account");



                    } else {
                        JOptionPane.showMessageDialog(this, "Email error", "Error", JOptionPane.ERROR_MESSAGE);
                    }

                } else {
                    JOptionPane.showMessageDialog(this, "Retype is not valid", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(txtCode, "Email is not valid", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed
        String env = System.getenv("USERPROFILE").toString();
        String pass = ReadWriteFile.read(env + File.separator + "p.phat");
        if ((pass.equals(txtPassActive.getText().trim()))
                && (txtPassNew.getText().trim().equals(txtRePassNew.getText().trim()))
                && !pass.equals(txtPassNew.getText().trim())) {

            ReadWriteFile.write(env + File.separator + "p.phat", txtPassNew.getText().trim());

            if (demo.langPo) {
                JOptionPane.showMessageDialog(this, "A senha foi atualizada com sucesso");
            } else {
                JOptionPane.showMessageDialog(this, "The password has been successfully updated");
            }


            txtPassActive.setText("");
            txtPassActive.setEditable(true);

            txtPassNew.setText("");
            txtRePassNew.setText("");

            btOk.setEnabled(false);


        } else {
            if (demo.langPo) {
                JOptionPane.showMessageDialog(this, "Senha atual incorreta");
            } else {
                JOptionPane.showMessageDialog(this, "Current password incorrect");
            }

        }

    }//GEN-LAST:event_btOkActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btOk;
    private javax.swing.JButton btSendEmail;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lbCode;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JLabel lbInfo;
    private javax.swing.JLabel lbReEmail;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtPassActive;
    private javax.swing.JTextField txtPassNew;
    private javax.swing.JTextField txtReEmail;
    private javax.swing.JTextField txtRePassNew;
    // End of variables declaration//GEN-END:variables
}
